package cn.scau.cmi.yeweijie.ORM.DAO;

import cn.scau.cmi.yeweijie.ORM.POJO.Student;
import cn.scau.cmi.yeweijie.ORM.POJO.Teacher;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Iterator;
import java.util.List;

public class StudentDAO {
    private static SessionFactory factory;
    public  StudentDAO(SessionFactory factory){
        this.factory=factory;
    }
    /* Method to CREATE an employee in the database */
    public Integer addStudent(int sid, String sname,int tid){
        Session session = factory.openSession();
        Transaction tx = null;
        Integer studentID = null;
        try{
            tx = session.beginTransaction();
            Student student = new Student(sid, sname,tid);
            studentID = (Integer) session.save(student);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
        return studentID;
    }
    /* Method to  READ all the employees */
    public void listStudent( ){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            List employees = session.createQuery("FROM Student ").list();
            for (Iterator iterator =
                 employees.iterator(); iterator.hasNext();){
                List teachers = session.createQuery("FROM Teacher ").list();
                Student employee = (Student) iterator.next();
                System.out.print("学号: " + employee.getSid());
                System.out.print("姓名: " + employee.getSname());
                for (Iterator iterator1 =
                     teachers.iterator(); iterator1.hasNext();){
                     Teacher teacher = (Teacher)iterator1.next();
                     if(employee.getTid()==teacher.getTid())
                         System.out.print("       老师是:"+teacher.getTname());
                }
            }
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
    /* Method to UPDATE salary for an employee */
    public void updateStudent(Integer sid, String sname ,Integer tid){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Student employee =
                    (Student) session.get(Student.class, sid);
            employee.setSname( sname );
            employee.setTid( tid );
            session.update(employee);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
    /* Method to DELETE an employee from the records */
    public void deleteStudent(Integer sid){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Student employee =
                    (Student) session.get(Student.class, sid);
            session.delete(employee);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
}
